/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package blue.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author CUTE
 */
public class DBUtil {
    private static Logger logger = Logger.getLogger(DBUtil.class.getName());

    /**
     * get a database connection
     * @return Connection
     */
    public static Connection getConnection() {
        Connection con = null;
        String driver = DBConfig.readKey("jdbcdriver");
        String sqlURL = DBConfig.readKey("jdbcurl");
        String sqlUser = DBConfig.readKey("jdbcusername");
        String sqlPassword = DBConfig.readKey("jdbcpassword");
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(sqlURL, sqlUser, sqlPassword);
            return con;
        } catch (ClassNotFoundException ex) {
            logger.log(Level.SEVERE, null, ex);
            return null;
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, ex);
            return null;
        }
    }


    public static void closeConnection(Connection con) {
        try {
            if (!con.isClosed()) {
                con.close();
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }

}
